package org.webscale.util;


public class TagUtil {
	
	
	public static String formatTagForPersist(String str){
		if(str != null){
			return str.toUpperCase().trim();
		}
		return null;
	}
	
	public static String formatForQuery(String str){
		return formatTagForPersist(str);
	}
	
	public static String formatTagForDisplaySearchResult(String str){
		if (str == null) {
			return null;
		}
		str = str.trim();
		StringBuilder sb = new StringBuilder();
		int wordCount = 0;
		boolean inSpace = true;
		int currentWordLength = 0;
		int lastWordLength = 0;
		char[] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (Character.isWhitespace(chars[i]) || chars[i] == '-') {
				if (inSpace) { // skipping duplicate spaces
					continue;
				}
				if (lastWordLength == 1 && currentWordLength == 1) {
					sb.setCharAt(sb.length() - 2, '.');
				}
				lastWordLength = currentWordLength;
				currentWordLength = 0; // add the whitespace
				sb.append(chars[i]);
				continue;
			}
			if (inSpace) { // beginning of next word
				wordCount++;
				inSpace = false;
				sb.append(Character.toUpperCase(chars[i]));
			} else {
				sb.append(Character.toLowerCase(chars[i]));
			}
			currentWordLength++;
		}
		int index;
		if (wordCount == 3 && (index = sb.indexOf(" & ")) != -1) {
			sb.replace(index, index + 3, "-");
		}
		return sb.toString();
	}
	
	/**
	 * Refer to TestCase 
	 * TestTagUtil for samples
	 * @param str
	 * @return
	 */
	public static String formatTagForDisplay(String str) {
		if (str == null) {
			return null;
		}
		str = str.trim();
		StringBuilder sb = new StringBuilder();
		int wordCount = 0;
		boolean inSpace = true;
		int currentWordLength = 0;
		int lastWordLength = 0;
		char[] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (Character.isWhitespace(chars[i]) || chars[i] == '-') {
				if (inSpace) { // skipping duplicate spaces
					continue;
				}
				inSpace = true; // check for "AR Rehman"
				if (currentWordLength == 2
						&& Character.isUpperCase(chars[i - 1])) {
					sb.setCharAt(sb.length() - 1, '.');
					sb.append(chars[i - 1]); // it's already uppercase
				}else if (lastWordLength == 1 && currentWordLength == 1) {
					sb.setCharAt(sb.length() - 2, '.');
				}
				lastWordLength = currentWordLength;
				currentWordLength = 0; // add the whitespace
				sb.append(chars[i]);
				continue;
			}
			if (inSpace) { // beginning of next word
				wordCount++;
				inSpace = false;
				sb.append(Character.toUpperCase(chars[i]));
			} else {
				sb.append(Character.toLowerCase(chars[i]));
			}
			currentWordLength++;
		}
		int index;
		if (wordCount == 3 && (index = sb.indexOf(" & ")) != -1) {
			sb.replace(index, index + 3, "-");
		}
		return sb.toString();
	}
	
	public static String[] getFirstLastNameFromFullName(String fullName){
		String [] names = new String[2];
		int firstIndex = fullName.indexOf(' ');
		int lastindex = fullName.lastIndexOf(' ');
		names[0] = null;
		names[1] = null;
		if(firstIndex > 0){
			names[0]= fullName.substring(0 , firstIndex);
		}
		if(lastindex > 0){
			names[1] = fullName.substring(lastindex);
		}
		return names;
	}
	
	public static String[] getFirstLastNameFromFullNameToUpperCase(String fullName){
		String[] names = getFirstLastNameFromFullName(fullName);
		if(names[0] != null){
			names[0] = names[0].toUpperCase();
		}
		if(names[1] != null){
			names[1] = names[1].toUpperCase();
		}
		return names;
	}
	
	/*
	public static String formatOldTag(String tag) {
		StringBuffer properInitials = new StringBuffer(tag);
		int ampIndex;
		Pattern initials_together = Pattern.compile("^[A-Z][A-Z] .*");
		Pattern initials_spaced = Pattern.compile("^[A-Z] [A-Z] .*");
		Pattern namePair = Pattern.compile("^[A-Z]+ & [A-Z]+$",
				Pattern.CASE_INSENSITIVE);
		Matcher fit_together = initials_together.matcher(tag);
		Matcher fit_spaced = initials_spaced.matcher(tag);
		Matcher fit_namePair = namePair.matcher(tag);
		String str1 = null;
		try {
			str1 = makeProper(tag);
			System.out.println(str1);
		} catch (IOException e)

		{

		}

		if (fit_together.matches()) {

			System.out.println(tag + " A match");

			properInitials.insert(1, ".");

			System.out.println(properInitials);

		} else if (fit_spaced.matches())

		{

			System.out.println(tag + " A match");

			// properInitials.insert(1, ".");

			properInitials.replace(1, 2, ".");

			System.out.println(properInitials);

		} else if (fit_namePair.matches())

		{

			System.out.println(properInitials + " A match");

			ampIndex = tag.indexOf('&');

			properInitials.replace(ampIndex, ampIndex + 1, "-");

			str1 = properInitials.toString();

			str1.trim();

			System.out.println(str1);

		}
		if(str1 == null){
			return tag;
		}else
		   return str1;
	}

	public static String makeProper(String theString)
			throws java.io.IOException {
		java.io.StringReader in = new java.io.StringReader(theString.toLowerCase());
		boolean precededBySpace = true;
		StringBuffer properCase = new StringBuffer();
		while (true) {
			int i = in.read();
			if (i == -1)
				break;
			char c = (char) i;
			if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/'
					|| c == '\\' || c == ',') {
				properCase.append(c);
				precededBySpace = true;
			} else {
				if (precededBySpace) {
					properCase.append(Character.toUpperCase(c));
				} else {
					properCase.append(c);
				}
				precededBySpace = false;
			}
		}
		return properCase.toString();
	}
*/
}
